<template>
  <div>
    <div class="i-layout-page-header">
      <PageHeader class="product_tabs" hidden-breadcrumb>
        <div slot="title">
          <span class="mr20">问卷列表</span>
        </div>
      </PageHeader>
    </div>
    <Card :bordered="false" dis-hover class="ivu-mt">
      <Row type="flex" class="mb20">
        <Col>
          <Button type="primary" icon="md-add" @click="add" class="mr10"
            >添加问卷</Button
          >
        </Col>
      </Row>
      <Table :columns="columns" :data="tableData" :loading="loading">
        <template slot-scope="{ row }" slot="add_time">
          {{ row.add_time | formatDate  }}
        </template>  
        <template slot-scope="{ row }" slot="status">
          <i-switch
            v-model="row.is_show"
            :value="row.is_show"
            :true-value="1"
            :false-value="0"
            @on-change="onchangeIsShow(row)"
            size="large"
          >
            <span slot="open">开启</span>
            <span slot="close">关闭</span>
          </i-switch>
        </template>
        <template slot-scope="{ row }" slot="action">
          <Button
            type="info"
            size="small"
            @click="handleView(row)"
            style="margin-right: 5px"
            >查看用户反馈</Button
          >
          <Button
            type="primary"
            size="small"
            @click="handleEdit(row)"
            style="margin-right: 5px"
            >编辑</Button
          >
          <Button type="error" size="small" @click="handleDelete(row, '删除')"
            >删除</Button
          >
        </template>
      </Table>

      <Page
        :total="total"
        :current="searchForm.page"
        :page-size="searchForm.limit"
        show-total
        @on-change="handlePageChange"
        style="margin-top: 20px; text-align: right"
      />
    </Card>
  </div>
</template>

<script>
import { surveyList,surveySethow } from "@/api/marketing"; 
import { formatDate } from "@/utils/validate"; 
export default {
    filters: {
        formatDate(time) {
        if (time !== 0) {
            let date = new Date(time * 1000);
            return formatDate(date, "yyyy-MM-dd hh:mm:ss");
        }
        },
    },
  data() {
    return {
      searchForm: {
        name: "",
        status: "",
        page: 1,
        limit: 10,
      },
      columns: [
        {
          title: "ID",
          key: "survey_id",
          width: 80,
        },
        {
          title: "活动名称",
          key: "title",
          minWidth: 150,
        },
        {
          title: "创建时间",
          slot: "add_time",
          width: 150,
        },
        {
          title: "状态",
          slot: "status",
          width: 200,
        },
        {
          title: "操作",
          slot: "action",
          width: 300,
        },
      ],
      tableData: [],
      total: 0,
      loading: false,
    };
  },
  methods: {
    // 添加
    add() {
      this.$router.push({ path: "/admin/marketing/feedback/problemSetting" });
    },
    handleSearch() {
      this.searchForm.page = 1;
      this.getList();
    },
    handleReset() {
      this.searchForm = {
        name: "",
        status: "",
        page: 1,
        limit: 10,
      };
      this.getList();
    },
    handlePageChange(page) {
      this.searchForm.page = page;
      this.getList();
    },
    handleView(row) {
      this.$router.push({
        path: "/admin/marketing/feedback/index/" + row.survey_id
      });
    },
    handleEdit(row) {
      this.$router.push({
        path: "/admin/marketing/feedback/problemSetting/" + row.survey_id
      });
    },
    handleDelete(row, tit) {
        let delfromData = {
          title: tit,
          url: `/survey/del/${row.survey_id}`,
          method: "DELETE",
        };
        this.$modalSure(delfromData)
          .then((res) => {
            this.$Message.success(res.msg);
            this.getList();
          })
          .catch((res) => {
            this.$Message.error(res.msg);
          });
    },
    // 列表
    getList() {
        console.log(this.searchForm,"123")
        this.loading = true;
        surveyList(this.searchForm)
          .then(async (res) => {
            let data = res.data;
            this.tableData = data.list;
            this.total = res.data.count;
            this.loading = false;
          })
          .catch((res) => {
            this.loading = false;
            this.$Message.error(res.msg);
          });
      },
    // 修改是否显示
    onchangeIsShow(row) {
        let data = {
          survey_id: row.survey_id,
          is_show: row.is_show,
        };
        surveySethow(data)
          .then(async (res) => {
            this.$Message.success(res.msg);
          })
          .catch((res) => {
            this.$Message.error(res.msg);
          });
      }
  },
  mounted() {
    this.getList();
  },
};
</script>

<style scoped></style>
